package com.hyt.it.ogt.ykcj.service.exam.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.ykcj.common.enums.BizErrorCode;
import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.common.utils.IdUtils;
import com.hyt.it.ogt.ykcj.common.utils.SecurityUtils;
import com.hyt.it.ogt.ykcj.domain.exam.ExamNameConfig;
import com.hyt.it.ogt.ykcj.domain.exam.ExamSubjectNameConfig;
import com.hyt.it.ogt.ykcj.domain.vo.ExamConfigNameVo;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamNameConfigMapper;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamSubjectNameConfigMapper;
import com.hyt.it.ogt.ykcj.service.exam.IExamNameConfigService;

/**
 * 考试名称配置Service业务层处理
 * 
 * @author admin
 * @date 2021-06-18
 */
@Service
public class ExamNameConfigServiceImpl extends BaseServiceImpl<ExamNameConfigMapper, ExamNameConfig> implements IExamNameConfigService
{
    @Autowired
    private ExamNameConfigMapper examNameConfigMapper;
    
    @Autowired
    private ExamInfoMapper examInfoMapper;
    
    @Autowired
    private ExamSubjectNameConfigMapper examSubjectNameConfigMapper;
    
    public String getExamSubjectCode(){
    	String code = null;
    	return code;
    }
    
    /**
     * 查询考试名称配置
     * 
     * @param id 考试名称配置ID
     * @return 考试名称配置
     */
    @Override
    public ExamNameConfig selectExamNameConfigById(String id)
    {
        return examNameConfigMapper.selectExamNameConfigById(id);
    }

    /**
     * 查询考试名称配置列表
     * 
     * @param examNameConfig 考试名称配置
     * @return 考试名称配置
     */
    @Override
    public List<ExamNameConfig> selectExamNameConfigList(ExamNameConfig examNameConfig)
    {
        return examNameConfigMapper.selectExamNameConfigList(examNameConfig);
    }

    /**
     * 新增考试名称配置
     * 
     * @param examNameConfig 考试名称配置
     * @return 结果
     */
    @Override
    public int insertExamNameConfig(ExamNameConfig examNameConfig)
    {
    	examNameConfig.setId(IdUtils.generateId());
    	examNameConfig.setExamNameCode(examNameConfig.getId());
        examNameConfig.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        return examNameConfigMapper.insertExamNameConfig(examNameConfig);
    }

    /**
     * 修改考试名称配置
     * 
     * @param examNameConfig 考试名称配置
     * @return 结果
     */
    @Override
    public int updateExamNameConfig(ExamNameConfig examNameConfig)
    {
        examNameConfig.setUpdateTime(DateUtils.getNowDate());
        return examNameConfigMapper.updateExamNameConfig(examNameConfig);
    }

    /**
     * 批量删除考试名称配置
     * 
     * @param ids 需要删除的考试名称配置ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteExamNameConfigByIds(String[] ids) throws Exception
    {
    	int res = 0;
    	//校验考试下是否已关联考试和科目
    	List<String> tempIds = Arrays.asList(ids);
    	Map<String,Object> parms = new HashedMap<>();
    	for (String configId : tempIds) {
    		parms.put("examNameConfigId",configId);
    		res = examInfoMapper.checkExamInfoByConfigId(parms);
		}
    	if(res >0){
    		 throw new Exception(BizErrorCode.EXAM_E0001.getErrMsg());
    	}else{
    		res = examNameConfigMapper.deleteExamNameConfigByIds(ids);
    	}
        return res;
    }

    /**
     * 删除考试名称配置信息
     * 
     * @param id 考试名称配置ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteExamNameConfigById(String id)
    {
        return examNameConfigMapper.deleteExamNameConfigById(id);
    }
    
    /**
     * 检查考试重名的数
     * @param @param config
     * @param @return
     */
    @Override
    public int checkExamNameIsExist(ExamNameConfig config){
    	Map<String,Object> parms = new HashedMap<>();
    	parms.put("name",config.getName());
    	parms.put("exam_type",config.getExamType());
    	return examNameConfigMapper.findSameNameConfigToCount(parms);
    }
    
    /**
     * 考试配置信息
     * @param @param examInfoVo
     * @param @return
     */
    @Override
    public List<ExamConfigNameVo> findAllExamConfig(String examType){
    	List<ExamConfigNameVo> vos = new ArrayList<>();

        ExamNameConfig examNameConfig1 = new ExamNameConfig();
        examNameConfig1.setExamType(examType);
        List<ExamNameConfig> examNames = examNameConfigMapper.selectExamNameConfigList(examNameConfig1);
//    	List<ExamNameConfig> examNames = examNameConfigMapper.selectExamNameConfigList(null);
    	if(CollectionUtils.isNotEmpty(examNames)){
    		for (ExamNameConfig examNameConfig : examNames) {
    			ExamConfigNameVo vo = new ExamConfigNameVo();
    			vo.setId(examNameConfig.getId());
    			vo.setName(examNameConfig.getName());
    			vo.setType("0");
    			vos.add(vo);
			}
    	}
    	
//    	List<ExamSubjectNameConfig> subNames = examSubjectNameConfigMapper.selectExamSubjectNameConfigList(null);
        List<ExamSubjectNameConfig> subNames = examSubjectNameConfigMapper.selectExamSubjectNameList(examType);
    	for (ExamSubjectNameConfig examSubjectNameConfig : subNames) {
    		ExamConfigNameVo vo = new ExamConfigNameVo();
    		vo.setId(examSubjectNameConfig.getId());
			vo.setName(examSubjectNameConfig.getName());
			vo.setExamNameId(examSubjectNameConfig.getExamNameId());
			vo.setType("1");
			vos.add(vo);
		}
    	return vos;
    }

    @Override
    public List<ExamConfigNameVo> getExamConfigList() {
        List<ExamNameConfig> list = examNameConfigMapper.selectExamNameConfigList(new ExamNameConfig());
        // 返回名称类别
        List<ExamConfigNameVo> configNameVoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)){
            for (ExamNameConfig config:list) {
                ExamConfigNameVo vo = new ExamConfigNameVo();
                vo.setId(config.getId());
                vo.setName(config.getName()+"("+config.getExamNameCode()+")");
                configNameVoList.add(vo);
            }
        }
        return configNameVoList;
    }
}
